﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using DevExpress.XtraSplashScreen;

namespace Baubixinh_client
{
    public partial class frmAddUser  : DevExpress.XtraEditors.XtraForm
    {
        public int UserID { get; set; }
        public string Username { get; set; }
        public string FullName { get; set; }
        public bool UserAdded { get; set; }
        public string UserPhone { get; set; }
        public string UserEmail { get; set; }
        public int UserStore { get; set; }

        public frmAddUser()
        {
            InitializeComponent();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            bool blnSuccess = true;

            try
            {
                if (!objValidation.Validate()) return;

                if (!clsCommon.fncMsgConfirm("Dữ liệu sẽ được lưu. Bạn có chắc chắn?")) return;

                SplashScreenManager.ShowForm(this, typeof(frmWaiting), true, true, false);

                int intID = clsDbAccess.fncGetMaxUser(Program.gobjDb) + 1;
                string strHoTen = txtHoTen.Text.Trim();
                string strEmail = txtEmail.Text.Trim();
                string strDienThoai = txtDienThoai.Text.Trim();
                int intKhoID = clsCommon.fncCnvNullToInt(txtKhoHang.EditValue);

                Program.gobjTrans = Program.gobjConn.BeginTransaction();
                try
                {
                    blnSuccess = blnSuccess && clsDbAccess.fncInsertUser(Program.gobjDb, Program.gobjTrans, intID, strHoTen, strEmail, strDienThoai, intKhoID);
                    blnSuccess = blnSuccess && clsDbAccess.fncUpdateUserStore(Program.gobjDb, Program.gobjTrans, intID, intKhoID);

                    if (blnSuccess) Program.gobjTrans.Commit();
                    else Program.gobjTrans.Rollback();
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    this.UserAdded = true;
                    this.UserID = intID;
                    this.Username = "";
                    this.FullName = strHoTen;
                    this.UserPhone = strDienThoai;
                    this.UserEmail = strEmail;
                    this.UserStore = intKhoID;

                    //clsCommon.fncMsgInfo("Thêm đơn hàng thành công.");
                    this.Close();
                }
                else
                {
                    clsCommon.fncMsgError("Thêm đơn hàng thất bại. Hãy kiểm tra lại dữ liệu.");
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { SplashScreenManager.CloseForm(false); }
        }

        private void frmAddUser_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable objStore;

                objStore = clsDbAccess.fncGetAttributeValue(Program.gobjDb, clsEnum.ATTRIBUTE.STORE);

                txtKhoHang.Properties.DataSource = objStore;

                txtHoTen.Focus();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}
